package com.hddf.project.auth.dao_impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.hddf.project.auth.dao.AuthorityDao;
import com.hddf.project.auth.pojos.Authority;


public class AuthorityDaoImpl implements AuthorityDao {

	public boolean delete(int id, Session s) {
		try{
			Authority auth = new Authority();
			auth.setId(id);
			s.delete(auth);
			auth = null;
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public Authority getAuthById(int id, Session s) {
		try{
			Authority auth = (Authority)s.get(Authority.class,id);
			return auth;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public Authority getAuthByName(String name, Session s) {
		try{
			List<Authority> list = null;
			Query query = s.createQuery("from Authority as auth where auth.name=:temp ");
			query.setString("temp",name);
			list = query.list();
			Authority auth = null;
			if(list!=null && list.size()>0){
				auth = list.get(0);
			}
			return auth;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Authority> getAuthorities(String start, String limit,int state,Session s) {
		try{
			List<Authority> list = null;
			if(state==1){
				state = 0;
			}else if(state==0){
				state = 1;
			}
			Query query = s.createQuery("from Authority as auth where auth.state<>:temp order by auth.sort asc ");
			query.setInteger("temp",state);
			int st = 0;
			int li = 0;
			if(start!=null && limit!=null){
				st = Integer.parseInt(start);
				li = Integer.parseInt(limit);
				query.setFirstResult(st);
				query.setMaxResults(li);
			}
			list = query.list();
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public int getAuthoritiesTotalCount(int state,Session s) {
		try{
			if(state==1){
				state = 0;
			}else if(state==0){
				state = 1;
			}
			Query query = s.createQuery("select count(id) from Authority as auth where auth.state<>:temp ");
			query.setInteger("temp",state);
			String rs = query.uniqueResult().toString();
			return Integer.parseInt(rs);
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}

	public boolean save(Authority auth, Session s) {
		try{
			s.save(auth);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean setAuthOff(int id, Session s) {
		try{
			Authority auth = (Authority)s.get(Authority.class,id);
			auth.setState(0);
			s.update(auth);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean setAuthOn(int id, Session s) {
		try{
			Authority auth = (Authority)s.get(Authority.class,id);
			auth.setState(1);
			s.update(auth);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean update(Authority auth, Session s) {
		try{
			s.update(auth);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

}
